package chattingRoomNIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
		SocketChannel sc=SocketChannel.open();
		sc.connect(new InetSocketAddress("localhost",8888));
		ByteBuffer  buffer=ByteBuffer.allocate(1024);
	//	while (true){
		//	sc.write(ByteBuffer.wrap("hello sever".getBytes()));
			//得到控制台的输入信息
		System.out.println("请输入你要发送的信息：");
		Scanner scanner=new Scanner(System.in);
		while (scanner.hasNext()){
			String sendMessage = scanner.nextLine();
			buffer.put(sendMessage.getBytes());
			//用来使用
			buffer.flip();
			//发送给服务器
			sc.write(buffer);
			buffer.clear();
		}
		sc.read(buffer);
		System.out.println(new String(buffer.array(),0,buffer.position()));
			//sc.close();
	//	}
	}
}